from django.test import TestCase

# Create your tests here.
import requests
import random

# Create your tests here.
main_url = "http://127.0.0.1:8000"


def random_name():
    import random
    xing = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜'
    ming = '豫章故郡洪都新府星分翼轸地接衡庐襟三江而带五湖'
    X = random.choice(xing)
    M = "".join(random.choice(ming) for i in range(2))
    print(X + M)
    return X + M


class ScoreTest(object):
    def __init__(self):
        self.post_name = random_name()

    def post(self):
        data = {
            "name": self.post_name,
            "score": random.randrange(1, 10000000),
        }
        response = requests.post(main_url + "/upload_data/", data=data)
        print(response.json())
        json_response = response.json()
        assert response.status_code == 200

    def get(self, start_, end_):
        data = {
            "name": self.post_name,
            "start_": start_,
            "end_": end_,
        }
        request_params = []
        for k, v in data.items():
            request_params.append(f"{k}={v}")

        print(request_params)
        response = requests.get(main_url + "/search/?{}".format("&".join(request_params)))
        print(response.json())
        json_response = response.json()
        assert response.status_code == 200
        assert json_response.get("code") == 200
        assert isinstance(json_response.get("data"), list)



if __name__ == '__main__':
    f = ScoreTest()
    f.post()
    f.get(1, 10)